<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.loli.net/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"monocy.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":true,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":false,"top_n_per_article":5,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="前言文章用来纪录我如何从零开始，通过 webpack4 发布一个 vue 的 npm 包，同时使用的是 es6 语法。涉及到的知识点包括：  webpack4 npm 包的发布 babel 配置 vue 配置">
<meta name="keywords" content="Vue,Webpack">
<meta property="og:type" content="article">
<meta property="og:title" content="发布一个 vue 包">
<meta property="og:url" content="https://monocy.gitee.io/2019/04/14/发布一个-vue-包/index.html">
<meta property="og:site_name" content="沐光">
<meta property="og:description" content="前言文章用来纪录我如何从零开始，通过 webpack4 发布一个 vue 的 npm 包，同时使用的是 es6 语法。涉及到的知识点包括：  webpack4 npm 包的发布 babel 配置 vue 配置">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2022-07-26T13:16:12.743Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="发布一个 vue 包">
<meta name="twitter:description" content="前言文章用来纪录我如何从零开始，通过 webpack4 发布一个 vue 的 npm 包，同时使用的是 es6 语法。涉及到的知识点包括：  webpack4 npm 包的发布 babel 配置 vue 配置">

<link rel="canonical" href="https://monocy.gitee.io/2019/04/14/发布一个-vue-包/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>发布一个 vue 包 | 沐光</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">沐光</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">记录在前端之路的点点滴滴</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">31</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">15</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">61</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/kazehaiya" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://monocy.gitee.io/2019/04/14/发布一个-vue-包/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="蔡胤">
      <meta itemprop="description" content="Just do IT!">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="沐光">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          发布一个 vue 包
        </h1>

        <div class="post-meta">
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-07-26 21:16:12" itemprop="dateModified" datetime="2022-07-26T21:16:12+08:00">2022-07-26</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Webpack/" itemprop="url" rel="index"><span itemprop="name">Webpack</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2019/04/14/发布一个-vue-包/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2019/04/14/发布一个-vue-包/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>3.4k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>3 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>文章用来纪录我如何从零开始，通过 webpack4 发布一个 vue 的 npm 包，同时使用的是 es6 语法。涉及到的知识点包括：</p>
<ul>
<li>webpack4</li>
<li>npm 包的发布</li>
<li>babel 配置</li>
<li>vue 配置</li>
</ul>
<a id="more"></a>
<h3 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h3><h4 id="了解相关知识"><a href="#了解相关知识" class="headerlink" title="了解相关知识"></a>了解相关知识</h4><p>首先需要了解相关基础知识，由简单到难，依次列举如下：</p>
<ol>
<li>如何发布一个 npm 包，需要准备些什么？</li>
<li>webpack 简单的打包配置需要哪些</li>
<li>如何配置 babel 的 es6 支持</li>
<li>如何配置 vue 的支持</li>
<li>如何配置 vue 的 @、@src 等引用</li>
<li>包内的 vue 的组件该如何编写</li>
<li>如何优化打包的体积</li>
</ol>
<h4 id="发包前的准备"><a href="#发包前的准备" class="headerlink" title="发包前的准备"></a>发包前的准备</h4><h5 id="创建账号"><a href="#创建账号" class="headerlink" title="创建账号"></a>创建账号</h5><p>发布 npm 包首先需要一个 npm 账号。npm 添加账号的方法：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">npm adduser</span><br><span class="line"><span class="comment">## 之后会提示你输入 userName 和 password</span></span><br><span class="line"><span class="comment">## ...</span></span><br><span class="line"><span class="comment">## 保存后登陆</span></span><br><span class="line">npm login</span><br></pre></td></tr></table></figure>
<h5 id="创建待发布的包"><a href="#创建待发布的包" class="headerlink" title="创建待发布的包"></a>创建待发布的包</h5><p>创建包的方法也十分简单，此处用 yarn 来创建一个 npm 包</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 首先进入对应文件夹，然后</span></span><br><span class="line">yarn init</span><br><span class="line"><span class="comment"># 之后根据提示输入项目名称、版本等内容即可</span></span><br></pre></td></tr></table></figure>
<h5 id="规范化-package-json-内容"><a href="#规范化-package-json-内容" class="headerlink" title="规范化 package.json 内容"></a>规范化 <code>package.json</code> 内容</h5><p>当然，要发布一个比较规范的包，仅默认的一些配置还是不够的，<code>package.json</code> 的详细配置可见<a href="http://javascript.ruanyifeng.com/nodejs/packagejson.html" target="_blank" rel="noopener">「中文文档」</a>，总结过后，我们可以配置这么一些字段：</p>
<ul>
<li><code>name</code>: 项目名称</li>
<li><code>version</code>: 当前包的版本</li>
<li><code>keywords</code>: 关键词，方便搜索</li>
<li><code>description</code>: 项目描述</li>
<li><code>repository</code>: 项目地址</li>
<li><code>author</code>: 作者</li>
<li><code>contributors</code>: 项目开发人员</li>
<li><code>files</code>: 项目包括的文件</li>
<li><code>scripts</code>: webpack 打包会用到</li>
<li><code>style</code>: 项目样式所存放的地址</li>
<li><code>license</code>: 许可证（一般 MIT 即可）</li>
</ul>
<p>安装时常用的有此三种依赖：</p>
<ul>
<li>dependencies: 必须依赖的包，会在用户安装此包时，将该部分的包一并装上，会更新版本（如果版本过低）</li>
<li>peerDependencies: 会在用户安装此包时，如果该模块内的包没有或者用户安装的对应包版本过低，会用 warning 提示用户安装（不会自动安装）</li>
<li>devDependencies: 开发时会用到的包，打包至项目的或者 webpack 配置需要的等等，不会帮用户安装或者给予提示</li>
</ul>
<p>它们<strong>依次</strong>对应的指令是：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># dependencies</span></span><br><span class="line">yarn add xxx</span><br><span class="line"></span><br><span class="line"><span class="comment"># peerDependencies</span></span><br><span class="line">yarn add xxx -P</span><br><span class="line"></span><br><span class="line"><span class="comment"># devDependencies</span></span><br><span class="line">yarn add xxx -D</span><br></pre></td></tr></table></figure>
<h3 id="初始化包的配置"><a href="#初始化包的配置" class="headerlink" title="初始化包的配置"></a>初始化包的配置</h3><p>两种安装方式</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 全局安装</span></span><br><span class="line">npm i -g webpack webpack-cli</span><br><span class="line"></span><br><span class="line"><span class="comment"># 项目安装</span></span><br><span class="line">yarn add webpack webpack-cli -D</span><br></pre></td></tr></table></figure>
<p>在先前创建的项目内新建一个 <code>webpack.config.js</code> 文件，并写入以下基础内容</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 处理路径</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">resolve</span>(<span class="params">dir</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> path.join(__dirname, dir);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  mode: <span class="string">'production'</span>,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注：webpack4 版本这两个是分开的，都得装</p>
</blockquote>
<h4 id="配置-webpack"><a href="#配置-webpack" class="headerlink" title="配置 webpack"></a>配置 webpack</h4><h5 id="出入口文件配置"><a href="#出入口文件配置" class="headerlink" title="出入口文件配置"></a>出入口文件配置</h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在 module.exports 文件内添加 entry 和 output 两字段</span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">  entry: &#123;</span><br><span class="line">    &lt;自定义名&gt;: &lt;文件地址&gt;</span><br><span class="line">  &#125;,</span><br><span class="line">  output: &#123;</span><br><span class="line">    path: &lt;输出地址&gt;,</span><br><span class="line">    publicPath: &lt;虚拟路径，server 路径有关&gt;,</span><br><span class="line">    filename: &lt;输出的文件名&gt;,</span><br><span class="line">    library: &lt;包名&gt;,</span><br><span class="line">    libraryTarget: &lt;库的支持，一般用 umd 即可&gt;,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">// 例子</span><br><span class="line">module.exports = &#123;</span><br><span class="line">  entry: &#123;</span><br><span class="line">    <span class="string">'test'</span>: <span class="string">'./src/index.js'</span></span><br><span class="line">  &#125;,</span><br><span class="line">  output: &#123;</span><br><span class="line">    path: resolve(<span class="string">'lib'</span>),</span><br><span class="line">    publicPath: <span class="string">'/lib/'</span>,</span><br><span class="line">    filename: <span class="string">'[name].js'</span>,</span><br><span class="line">    library: <span class="string">'test'</span>,</span><br><span class="line">    libraryTarget: <span class="string">'umd'</span></span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="loader-配置"><a href="#loader-配置" class="headerlink" title="loader 配置"></a>loader 配置</h5><h6 id="vue-loader"><a href="#vue-loader" class="headerlink" title="vue-loader"></a>vue-loader</h6><p>由于项目是关于 vue 的，因此可参考<a href="https://vue-loader.vuejs.org/zh/" target="_blank" rel="noopener">「vue-loader 官网」</a>，跟着对应内容配置即可，解析 vue 的 loader 配置</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// rules 部分</span></span><br><span class="line">&#123;</span><br><span class="line">  test: <span class="regexp">/\.vue$/</span>,</span><br><span class="line">  loader: <span class="string">'vue-loader'</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// plugins 部分</span></span><br><span class="line">plugins: [</span><br><span class="line">  <span class="keyword">new</span> VueLoaderPlugin()</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="comment">// resolve 部分</span></span><br><span class="line">resolve: &#123;</span><br><span class="line">  <span class="comment">// 设置别名</span></span><br><span class="line">  alias: &#123;</span><br><span class="line">    vue$: <span class="string">'vue/dist/vue.esm.js'</span>,</span><br><span class="line">     <span class="string">'@'</span>: resolve(<span class="string">'./src'</span>),</span><br><span class="line">    <span class="string">'@mixins'</span>: resolve(<span class="string">'./src/mixins'</span>),</span><br><span class="line">    <span class="string">'@base'</span>: resolve(<span class="string">'./src/base'</span>),</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 文件后缀扩展识别（导入时自动加后缀）</span></span><br><span class="line">  extensions: [<span class="string">'*'</span>, <span class="string">'.js'</span>, <span class="string">'.vue'</span>]</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h6 id="es6-支持"><a href="#es6-支持" class="headerlink" title="es6 支持"></a>es6 支持</h6><p>首先在 <code>webpack</code> 配置文件的 <code>rules</code> 部分(<code>vue-loader</code> 配置的部分)添加 <code>js</code> 的语法转译</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ...</span></span><br><span class="line">rules: [</span><br><span class="line">  &#123;</span><br><span class="line">    test: <span class="regexp">/\.js$/</span>,</span><br><span class="line">    loader: <span class="string">'babel-loader'</span>,</span><br><span class="line">    include: [resolve(<span class="string">'src'</span>)],</span><br><span class="line">    exclude: [resolve(<span class="string">'node_modules'</span>)],</span><br><span class="line">  &#125;,</span><br><span class="line">];</span><br></pre></td></tr></table></figure>
<p>然后在项目内添加 <code>.babelrc</code> 文件，并在内部添加 <code>es</code> 支持</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"presets"</span>: [<span class="string">"@babel/preset-env"</span>],</span><br><span class="line">  <span class="attr">"plugins"</span>: [<span class="string">"@babel/plugin-transform-runtime"</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此处使用的是最新版本的 babel 配置，官网有详细的讲解</p>
<ul>
<li><a href="https://babeljs.io/docs/en/babel-preset-env" target="_blank" rel="noopener">@babel/preset-env</a></li>
<li><a href="https://babeljs.io/docs/en/babel-plugin-transform-runtime" target="_blank" rel="noopener">@babel/plugin-transform-runtime</a></li>
</ul>
<p>需要安装的一些依赖：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// devDependencies 部分</span></span><br><span class="line"><span class="string">"@babel/core"</span>: <span class="string">"^7.4.3"</span>,</span><br><span class="line"><span class="string">"@babel/plugin-transform-runtime"</span>: <span class="string">"^7.4.3"</span>,</span><br><span class="line"><span class="string">"@babel/preset-env"</span>: <span class="string">"^7.4.3"</span>,</span><br><span class="line"><span class="string">"babel-core"</span>: <span class="string">"^7.0.0-bridge.0"</span>,</span><br><span class="line"><span class="string">"babel-loader"</span>: <span class="string">"^7.1.2"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// dependencies 部分</span></span><br><span class="line"><span class="string">"@babel/runtime"</span>: <span class="string">"^7.4.3"</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意： babel-core 默认安装的是 6.x 版本，装 7 版本的命令是： <code>yarn add babel-core@^7.0.0-bridge.0 -D</code></p>
</blockquote>
<h6 id="css-样式支持"><a href="#css-样式支持" class="headerlink" title="css 样式支持"></a>css 样式支持</h6><p>由于 vue 项目中用的较多的是 less 或者是 sass，因此打包时需要对样式进行转译，此时需要样式相关的 loader。webpack 的配置例子如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// rules 部分</span></span><br><span class="line">&#123;</span><br><span class="line">  test: <span class="regexp">/\.(le|sc|sa|c)ss$/</span>,</span><br><span class="line">  use: [</span><br><span class="line">    MiniCssExtractPlugin.loader,</span><br><span class="line">    <span class="string">'css-loader'</span>,</span><br><span class="line">    <span class="string">'postcss-loader'</span>,</span><br><span class="line">    <span class="string">'less-loader'</span></span><br><span class="line">  ],</span><br><span class="line">  include: [ resolve(<span class="string">'src'</span>) ]</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// plugins 部分</span></span><br><span class="line">plugins: [</span><br><span class="line">  <span class="comment">// 提取 CSS 至单一文件夹</span></span><br><span class="line">  <span class="keyword">new</span> MiniCssExtractPlugin(&#123;</span><br><span class="line">    filename: <span class="string">'css/[name].[hash].css'</span>,</span><br><span class="line">    chunkFilename: <span class="string">'[id].[hash].css'</span>,</span><br><span class="line">  &#125;)</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<p>这里注意到最顶部配置的不是 <code>style-loader/vue-style-loader</code>，当我们使用的是开发环境即：<code>&quot;mode&quot;: &quot;development&quot;</code> 时，使用 <code>style-loader/vue-style-loader</code>，当使用生产环境即：<code>&quot;mode&quot;: &quot;production&quot;</code> 时，我们使用此处的配置，以达到最小化压缩</p>
<h6 id="其余资源支持"><a href="#其余资源支持" class="headerlink" title="其余资源支持"></a>其余资源支持</h6><p>参考官方文档总结的配置如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  test: <span class="regexp">/\.(png|jpeg|jpg|gif|svg)$/i</span>,</span><br><span class="line">  loader: <span class="string">'url-loader'</span>,</span><br><span class="line">  options: &#123;</span><br><span class="line">    limit: <span class="number">8192</span>,</span><br><span class="line">    fallback: <span class="string">'file-loader'</span>,</span><br><span class="line">    name: <span class="string">'[name].[ext]?[hash]'</span>,</span><br><span class="line">    outputPath: <span class="string">'images/'</span>,</span><br><span class="line">    publicPath: <span class="string">''</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>url-loader 转为 base64 格式，对于小文件类型用这种方法处理更优，但是大文件最好还是用 file-loader 处理，因此对此 loader 设定一个限制，大于阈值 8K 的自动用 file-loader 处理</p>
<blockquote>
<p><a href="https://webpack.docschina.org/loaders/url-loader/#getting-started" target="_blank" rel="noopener">文档传送门</a></p>
</blockquote>
<h5 id="optimization-配置"><a href="#optimization-配置" class="headerlink" title="optimization 配置"></a>optimization 配置</h5><p>此部分是最后对文件进行压缩优化一下体积，webpack4 中生产环节相对较好的 <code>devtool</code> 模式是 <code>cheap-module-source-map</code>，但是压缩出的文件内会包含 <code>source-map</code>，如果想将其去掉需要额外的一些配置。webpack 配置例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">optimization: &#123;</span><br><span class="line">  minimizer: [</span><br><span class="line">    <span class="keyword">new</span> UglifyJsPlugin(&#123;</span><br><span class="line">      uglifyOptions: &#123;</span><br><span class="line">        <span class="comment">// 最快压缩模式</span></span><br><span class="line">        compress: <span class="literal">false</span>,</span><br><span class="line">        mangle: <span class="literal">true</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;),</span><br><span class="line">    <span class="keyword">new</span> OptimizeCSSAssetsPlugin(&#123;&#125;)</span><br><span class="line">  ],</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此处使用的是 UglifyJsPlugin 压缩 js 代码，删除 map 部分的内容， OptimizeCssAssetsPlugin 则是压缩 CSS 部分的内容，具体配置可参考对应的 npm 插件文档</p>
<ul>
<li><a href="https://www.npmjs.com/package/uglifyjs-webpack-plugin" target="_blank" rel="noopener">uglify 部分</a></li>
<li><a href="https://www.npmjs.com/package/optimize-css-assets-webpack-plugin" target="_blank" rel="noopener">css 部分</a></li>
</ul>
<blockquote>
<p>注：css 部分有推荐配合使用 <code>cssnano</code>，因为本项目 css 内容就 2 属性，因此不做过多的配置了，有需要的可查阅相关知识配置即可</p>
</blockquote>
<h6 id="附带的配置"><a href="#附带的配置" class="headerlink" title="附带的配置"></a>附带的配置</h6><ul>
<li>clean-webpack-plugin: 放在 plugins 的最后面，可每次自动清除对应的生成文件</li>
<li>webpack-bundle-analyzer: 对压缩的文件的依赖树的可视化分析，方便分析是否有多余依赖</li>
</ul>
<h4 id="vue-的配置"><a href="#vue-的配置" class="headerlink" title="vue 的配置"></a>vue 的配置</h4><p>发布一个 vue 组件时需要为组件添加 install 方法，每个组件的文件目录树大致为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">fileName</span><br><span class="line">  |-- index.js</span><br><span class="line">  |-- src</span><br><span class="line">       | -- component</span><br></pre></td></tr></table></figure>
<p>这是一个组件的文件结构，其中的 <code>component</code> 就是我们的 vue 组件，<code>index.js</code> 为向外侧暴露该组件的一个入口文件，其内容基本格式一致，例如：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> myComponent <span class="keyword">from</span> <span class="string">'./src/component'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 添加单独引用的 install 方法</span></span><br><span class="line">myComponent.install = <span class="function"><span class="keyword">function</span> (<span class="params">Vue, options = &#123;&#125;</span>) </span>&#123;</span><br><span class="line">  Vue.component(myComponent.name, myComponent);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> myComponent;</span><br></pre></td></tr></table></figure>
<p>整体的配置可以参考 <a href="https://github.com/ElemeFE/element" target="_blank" rel="noopener">elementUI</a> 的组件写法，原理一样，本质是如何添加 install 方法</p>
<h3 id="开发时的一些疑问"><a href="#开发时的一些疑问" class="headerlink" title="开发时的一些疑问"></a>开发时的一些疑问</h3><h4 id="关于-devtool-的选用"><a href="#关于-devtool-的选用" class="headerlink" title="关于 devtool 的选用"></a>关于 <code>devtool</code> 的选用</h4><p>开发环境推荐： <code>cheap-module-eval-source-map</code></p>
<p>生产环境推荐：<code>cheap-module-source-map</code></p>
<p>理由：</p>
<ul>
<li>大部分情况我们调试并不关心列信息，而且就算 sourcemap 没有列，有些浏览器引擎（例如 v8） 也会给出列信息，所以我们使用 cheap 模式可以大幅提高 souremap 生成的效率</li>
<li>使用 module 可支持 babel 这种预编译工具（在 webpack 里做为 loader 使用）</li>
<li>使用 eval 方式可大幅提高持续构建效率，参考 <a href="https://www.webpackjs.com/configuration/devtool/#devtool" target="_blank" rel="noopener">webapck devtool</a> 速度对比列表，这对经常需要边改边调的前端开发而言非常重要</li>
<li>直接将 sourceMap 放入打包后的文件，会明显增大文件的大小，不利于静态文件的快速加载；而外联 .map 时，.map 文件只会在 F12 开启时进行下载（ sourceMap 主要服务于调试），故推荐使用外联 .map 的形式</li>
</ul>
<h4 id="升级-babel-的问题"><a href="#升级-babel-的问题" class="headerlink" title="升级 babel 的问题"></a>升级 babel 的问题</h4><p>依照官方文档的介绍，安装了 <code>@babel/core</code> 等插件，并更新了 <code>.babelrc</code> 的配置，但是 build 的时候报错表示 babel-core 版本过低（直接 <code>yarn add babel-core</code> 装的是 6.x 版本），需要 7.0.0.0 版本，因此需要更新 <code>babel-core</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add babel-core@^7.0.0-bridge.0 -D</span><br></pre></td></tr></table></figure>
<h4 id="本地调试包"><a href="#本地调试包" class="headerlink" title="本地调试包"></a>本地调试包</h4><p>一般通过 <code>yarn link</code> 来调试编写的包，但是除此之外还有另外一个方法，通过相对路径来安装对应的包</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">"target-file": "file:&lt;relative-path&gt;"</span><br></pre></td></tr></table></figure>
<p>但是缺点是，每次更新后都需要删除后重新安装此包，或者将包升级一下版本，然后再次安装才能调试，比较麻烦。</p>
<p>最好的配置方法还是通过 <code>yarn link</code> 来调试。</p>
<h4 id="按需引入比解构引入打包的体积要大？"><a href="#按需引入比解构引入打包的体积要大？" class="headerlink" title="按需引入比解构引入打包的体积要大？"></a>按需引入比解构引入打包的体积要大？</h4><p>分析了一下打包后的代码，就用以下的例子来解释</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 按需</span></span><br><span class="line"><span class="keyword">import</span> debouce <span class="keyword">from</span> <span class="string">'lodash/debounce'</span>;</span><br><span class="line"><span class="comment">// 解构</span></span><br><span class="line"><span class="keyword">import</span> &#123; debounce &#125; <span class="keyword">from</span> <span class="string">'lodash'</span>;</span><br></pre></td></tr></table></figure>
<p>通过 webpack-bundle-analyzer 分析打包后的内容，发现前者有 lodash 的模块，后者没有，后者没有的原因时在压缩的代码内有 <code>require(&#39;lodash&#39;)</code> 即，我们需要配置 <code>package.json</code> 的 <code>lodash</code> 的 <code>dependencies</code> 依赖，需要引用此包的项目有对 <code>lodash</code> 的依赖，也就是说，解构获取的 <code>debouce</code> 不会将对应的内容连同代码一起打包，而是需要用户安装对应的包的依赖，因此体积相对会变小。</p>
<h4 id="为什么要用-library？"><a href="#为什么要用-library？" class="headerlink" title="为什么要用 library？"></a>为什么要用 library？</h4><p>使用 umd 是为了兼容 commonjs 和 amd 的方式，一般发包用 umd 可保证不会出啥问题，兼容性也很棒，而使用 umd 则需要配置 library。</p>
<h4 id="path-和-publicPath"><a href="#path-和-publicPath" class="headerlink" title="path 和 publicPath"></a>path 和 publicPath</h4><p>一般我们配置好 path 其实就能万事大吉了，那 publicPath 究竟有什么用呢？其实简单理解， path 至当前的项目的根目录（本地），publicPath 指的是服务器的根目录（服务器）,比如：将资源放在 CDN 上时，把 publicPath 设置为 CDN 的值就行了。</p>
<h3 id="参考文档"><a href="#参考文档" class="headerlink" title="参考文档"></a>参考文档</h3><ul>
<li><a href="https://www.cnblogs.com/hhhyaaon/p/5657469.html" target="_blank" rel="noopener">webpack devtool 配置对比</a></li>
<li><a href="https://www.webpackjs.com/concepts/" target="_blank" rel="noopener">webpack 文档</a></li>
<li><a href="https://webpack.docschina.org/configuration/#%E9%80%89%E9%A1%B9" target="_blank" rel="noopener">webpack 配置详情文档</a></li>
<li><a href="https://www.tangshuang.net/3343.html" target="_blank" rel="noopener">webpack externals 详解</a></li>
<li><a href="https://www.cnblogs.com/SamWeb/p/8353367.html" target="_blank" rel="noopener">webpack 配置 publicPath 的理解</a></li>
<li><a href="https://www.babeljs.cn/docs/" target="_blank" rel="noopener">babel 文档</a></li>
<li><a href="https://github.com/babel/babel-bridge" target="_blank" rel="noopener">babel-bridge</a></li>
<li><a href="https://vue-loader.vuejs.org/zh/" target="_blank" rel="noopener">vue-loader</a></li>
</ul>
<h3 id="附录"><a href="#附录" class="headerlink" title="附录"></a>附录</h3><p><a href="https://github.com/kazehaiya/vue-component" target="_blank" rel="noopener">完整样例</a></p>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>蔡胤
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://monocy.gitee.io/2019/04/14/发布一个-vue-包/" title="发布一个 vue 包">https://monocy.gitee.io/2019/04/14/发布一个-vue-包/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/Vue/" rel="tag"><i class="fa fa-tag"></i> Vue</a>
              <a href="/tags/Webpack/" rel="tag"><i class="fa fa-tag"></i> Webpack</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2019/04/06/windows-node-环境配置/" rel="prev" title="node 环境配置（windows）">
      <i class="fa fa-chevron-left"></i> node 环境配置（windows）
    </a></div>
      <div class="post-nav-item">
    <a href="/2019/04/18/浅谈行内元素/" rel="next" title="浅谈行内元素">
      浅谈行内元素 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#准备工作"><span class="nav-number">2.</span> <span class="nav-text">准备工作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#了解相关知识"><span class="nav-number">2.1.</span> <span class="nav-text">了解相关知识</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#发包前的准备"><span class="nav-number">2.2.</span> <span class="nav-text">发包前的准备</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#创建账号"><span class="nav-number">2.2.1.</span> <span class="nav-text">创建账号</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#创建待发布的包"><span class="nav-number">2.2.2.</span> <span class="nav-text">创建待发布的包</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#规范化-package-json-内容"><span class="nav-number">2.2.3.</span> <span class="nav-text">规范化 package.json 内容</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#初始化包的配置"><span class="nav-number">3.</span> <span class="nav-text">初始化包的配置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#配置-webpack"><span class="nav-number">3.1.</span> <span class="nav-text">配置 webpack</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#出入口文件配置"><span class="nav-number">3.1.1.</span> <span class="nav-text">出入口文件配置</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#loader-配置"><span class="nav-number">3.1.2.</span> <span class="nav-text">loader 配置</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#vue-loader"><span class="nav-number">3.1.2.1.</span> <span class="nav-text">vue-loader</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#es6-支持"><span class="nav-number">3.1.2.2.</span> <span class="nav-text">es6 支持</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#css-样式支持"><span class="nav-number">3.1.2.3.</span> <span class="nav-text">css 样式支持</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#其余资源支持"><span class="nav-number">3.1.2.4.</span> <span class="nav-text">其余资源支持</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#optimization-配置"><span class="nav-number">3.1.3.</span> <span class="nav-text">optimization 配置</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#附带的配置"><span class="nav-number">3.1.3.1.</span> <span class="nav-text">附带的配置</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#vue-的配置"><span class="nav-number">3.2.</span> <span class="nav-text">vue 的配置</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#开发时的一些疑问"><span class="nav-number">4.</span> <span class="nav-text">开发时的一些疑问</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#关于-devtool-的选用"><span class="nav-number">4.1.</span> <span class="nav-text">关于 devtool 的选用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#升级-babel-的问题"><span class="nav-number">4.2.</span> <span class="nav-text">升级 babel 的问题</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#本地调试包"><span class="nav-number">4.3.</span> <span class="nav-text">本地调试包</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#按需引入比解构引入打包的体积要大？"><span class="nav-number">4.4.</span> <span class="nav-text">按需引入比解构引入打包的体积要大？</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#为什么要用-library？"><span class="nav-number">4.5.</span> <span class="nav-text">为什么要用 library？</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#path-和-publicPath"><span class="nav-number">4.6.</span> <span class="nav-text">path 和 publicPath</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#参考文档"><span class="nav-number">5.</span> <span class="nav-text">参考文档</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#附录"><span class="nav-number">6.</span> <span class="nav-text">附录</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="蔡胤"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">蔡胤</p>
  <div class="site-description" itemprop="description">Just do IT!</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">61</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">31</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/kazehaiya" title="GitHub → https://github.com/kazehaiya" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:c763416559@163.com" title="Mail → mailto:c763416559@163.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/Nora_Monocy" title="Weibo → https://weibo.com/Nora_Monocy" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i></a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://liujinkai.com/" title="http://liujinkai.com/" rel="noopener" target="_blank">凯凯刘</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://weekly.75team.com/" title="https://weekly.75team.com/" rel="noopener" target="_blank">奇舞周刊</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://75.team/post/list" title="https://75.team/post/list" rel="noopener" target="_blank">奇舞团博客</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tech.meituan.com/" title="https://tech.meituan.com/" rel="noopener" target="_blank">美团技术团队</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tech.youzan.com/" title="https://tech.youzan.com/" rel="noopener" target="_blank">有赞技术团队</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://kalacloud.com" title="https://kalacloud.com" rel="noopener" target="_blank">卡拉云</a>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn" rel="noopener" target="_blank">京ICP备2020036147号-1 </a>
      <img src="/images/beian.png" style="display: inline-block;"><a href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=42011702000651" rel="noopener" target="_blank">鄂公网安备 42011702000651 号 </a>
  </div>

<div class="copyright">
  
  &copy; 2018 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-dragon"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">蔡胤</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">101k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">1:32</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/theme-next/theme-next-pjax@0/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/medium-zoom@1/dist/medium-zoom.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>
  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>




  




  <script src="/js/local-search.js"></script>












    <div id="pjax">
  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/valine@1/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : true,
      notify     : true,
      appId      : 'GkgJm97N3UFm2mRb1eFcP2dn-gzGzoHsz',
      appKey     : 'kJ8tYUUkX4ifhWha8rjnxWS8',
      placeholder: "请不要吝惜你的笔头，来说点什么吧！",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : true,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

    </div>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/Epsilon2.1.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true,"scale":0.5},"react":{"opacity":0.7},"log":false});</script></body>
</html>
